package uk.co.q3c.deplan.server.admin;

import uk.co.q3c.deplan.client.dao.CalendarService;
import uk.co.q3c.deplan.client.dao.DesignService;
import uk.co.q3c.deplan.client.dao.ResourceService;
import uk.co.q3c.deplan.client.service.ServiceFactory;
import uk.co.q3c.deplan.client.service.TaskService;
import uk.co.q3c.deplan.server.dao.AdminDao;
import uk.co.q3c.deplan.server.dao.AdminDao_BigT;
import uk.co.q3c.deplan.server.dao.DomainDao_BigT;
import uk.co.q3c.deplan.server.service.CalendarService_BigT;
import uk.co.q3c.deplan.server.service.ResourceService_BigT;
import uk.co.q3c.deplan.server.service.TaskService_BigT;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;

public class ServerServiceFactory implements ServiceFactory {

	private AdminDao adminDao;
	private CalendarService calendarService;
	private DesignService designService;
	private ResourceService resourceService;
	private TaskService taskService;

	private Objectify ofy;

	public ServerServiceFactory() {
		super();
	}

	public Objectify ofy() {
		if (ofy == null) {
			ObjectifyOpts opts = new ObjectifyOpts().setSessionCache(true);
			ofy = ObjectifyService.begin(opts);
		}
		return ofy;
	}

	public AdminDao adminDao() {
		if (adminDao == null) {
			adminDao = new AdminDao_BigT(this);
		}
		return adminDao;
	}

	@Override
	public CalendarService calendarService() {
		if (calendarService == null) {
			calendarService = new CalendarService_BigT(new DomainDao_BigT(this));
		}
		return calendarService;
	}

	@Override
	public DesignService designService() {
		if (designService == null) {
			// designDao = new DesignDao_BigT(this);
		}
		return designService;
	}

	@Override
	public ResourceService resourceService() {
		if (resourceService == null) {
			resourceService = new ResourceService_BigT(new DomainDao_BigT(this));
		}
		return resourceService;
	}

	@Override
	public TaskService taskService() {
		if (taskService == null) {
			taskService = new TaskService_BigT(new DomainDao_BigT(this));
		}
		return taskService;
	}

	@Override
	public void closeSession() {
		ofy = null;
	}

	@Override
	public void openSession() {
		ofy();
	}

}
